package com.airpi.activities;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.airpi.R;
import com.airpi.connectivity.RaspberryPiDiscoverer;
import com.airpi.heavy_tasks.Connexion;

/**
 * Activity in charge of the modification of the Admin and Guest passwords 
 *
 */

public class PasswordsActivity extends Activity {
	Button b;
	EditText currentAdminPassword;
	EditText newAdminPassword;
	EditText currentGuestPassword;
	EditText newGuestPassword;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.passwords);
		getActionBar().setDisplayHomeAsUpEnabled(true);

		b = (Button) findViewById(R.id.apply_changes_button);
		b.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AsyncTaskConnect atc = new AsyncTaskConnect(PasswordsActivity.this);
				atc.execute();
			}
		});

		currentAdminPassword = (EditText) findViewById(R.id.admin_current_password_edittext);
		newAdminPassword = (EditText) findViewById(R.id.admin_new_password_edittext);
		currentGuestPassword = (EditText) findViewById(R.id.guest_current_password_edittext);
		newGuestPassword = (EditText) findViewById(R.id.guest_new_password_edittext);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return false; // false hides the menu
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
		case android.R.id.home:
			finish();
			break;
		}
		return true;
	}

	private class AsyncTaskConnect extends AsyncTask<Void, Integer, String>{
		private Activity activity;

		private AsyncTaskConnect(Activity activity){
			this.activity = activity;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected void onProgressUpdate(Integer... values){
			super.onProgressUpdate(values);
		}

		@Override
		protected String doInBackground(Void... arg) {
			SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
			String ip = sharedPrefs.getString(RaspberryPiDiscoverer.PREFS_LAST_FOUND, null);//TODO changer pour PREFS_LAST_FOUND

			String caP = currentAdminPassword.getText().toString();
			String naP = newAdminPassword.getText().toString();
			String cgP = currentGuestPassword.getText().toString();
			String ngP = newGuestPassword.getText().toString();

			Connexion co = new Connexion(ip);

			if((caP.length() == 4 && naP.length() == 4))
			{
				if((cgP.length() == 4) && (ngP.length() == 4)){
					Log.e("1.1", cgP);
					Log.e("1.1", ngP);
					co.sendToServer("AdminPassword:"+caP+";"+naP);
					co.sendToServer("GuestPassword:"+cgP+";"+ngP);
					System.out.println(co.answerFromServer());
					return "bothNotNull";
				}
				else{
					Log.e("1.2", caP);
					Log.e("1.2", naP);
					co.sendToServer("AdminPassword:"+caP+";"+naP);
					System.out.println(co.answerFromServer());
					return "adminNotNull";
				}				
			}
			else{
				if((cgP.length() == 4) && (ngP.length() == 4)){
					Log.e("2.1", caP);
					Log.e("2.1", naP);
					co.sendToServer("GuestPassword:"+cgP+";"+ngP);
					System.out.println(co.answerFromServer());
					return "guestNotNull";
				}
				else {
					Log.e("2.2", cgP);
					Log.e("2.2", ngP);
					return "bothNull";
				}
			}
		}

		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if(result.equals("bothNull")){
				Toast toast = Toast.makeText(activity, "Please fill at least two of the fields ", Toast.LENGTH_LONG);
				toast.show();
			}
			else if(result.equals("bothNotNull")){
				Toast toast = Toast.makeText(activity, "The two fields are filled", Toast.LENGTH_SHORT);
				toast.show();
			}
			else if (result.equals("adminNotNull")){
				Toast toast = Toast.makeText(activity, "Admin Not Null & Guest Null", Toast.LENGTH_SHORT);
				toast.show();
			}
			else{
				Toast toast = Toast.makeText(activity, "Guest Not Null & Admin Null", Toast.LENGTH_SHORT);
				toast.show();
			}
		}
	}
}
